package com.cskaoyan.service;


import com.cskaoyan.bean.user.User;
import com.cskaoyan.bean.user.UserExample;
import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.LoginUserData;

import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;


    @SuppressWarnings("all")
    @Override
    public BaseData<User> query(BaseParam param, String username, String mobile) {
//分页预编译
        PageUtil.start(param);

        UserExample example = new UserExample();
        example.setOrderByClause(param.getSort() + " " + param.getOrder());
        UserExample.Criteria criteria = example.createCriteria();
        if (username != null && !"".equals(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (mobile != null && !"".equals(mobile)) {
            criteria.andMobileEqualTo(mobile);
        }
        List<User> users = userMapper.selectByExample(example);
        PageInfo<User> pageInfo = new PageInfo<>(users);

        BaseData<User> data = new BaseData<>(pageInfo, users);
        return data;
    }

    @Override
    public LoginUserData login(Object username, Object password) {
        return null;
    }

    @Override
    public User detail(Integer id) {
        User user = userMapper.selectByPrimaryKey(id);
        return user;
    }

    @Override
    public void update(User user) {
        userMapper.updateByPrimaryKey(user);

    }

    @Override
    public User getUser(String username) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<User> users = userMapper.selectByExample(userExample);
        if (users.size() != 0) {
            return users.get(0);
        }
        return null;
    }
}
